{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.5  0.5]\n",
      "1.11022302463e-16\n",
      "[ 3.5]\n"
     ]
    }
   ],
   "source": [
    "#scikit-learn example\n",
    "from sklearn.linear_model.base import LinearRegression\n",
    "X = [[0, 0], [1, 1], [2, 2]]           #三个样本，每个样本两个特征\n",
    "y = [0,1,2]                            #y值\n",
    "clf = LinearRegression() #还有其他可选参数，参加笔记\n",
    "clf.fit(X,y)\n",
    "print clf.coef_      #参数\n",
    "print clf.intercept_  #截距\n",
    "\n",
    "print clf.predict([[3,4]])  #预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 0 9 0]\n",
      " [0 7 0 0]\n",
      " [0 0 0 0]\n",
      " [0 0 0 5]]\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "#稀疏矩阵\n",
    "import numpy as np\n",
    "from scipy import sparse\n",
    "\n",
    "row  = np.array([0, 3, 1, 0])\n",
    "col  = np.array([0, 3, 1, 2])\n",
    "data = np.array([4, 5, 7, 9])\n",
    "x_sparse = sparse.coo_matrix((data, (row, col)), shape=(4, 4))\n",
    "x_array = x_sparse.toarray()\n",
    "print x_array\n",
    "print sparse.issparse(x_sparse)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [2 3 4]]\n"
     ]
    }
   ],
   "source": [
    "##np.vstack 纵向合并两个array\n",
    "a = np.array([1, 2, 3])\n",
    "b = np.array([2, 3, 4])\n",
    "print np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "-------------------------\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "[ 0  1  2  3  4  5  6  7  8  9 10 11]\n",
      "-------------------------\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "##np.ravel 拉直数据\n",
    "a = np.arange(12).reshape((3,4))\n",
    "print a\n",
    "print '-------------------------'\n",
    "print np.ravel(a)\n",
    "\n",
    "print a.ravel()\n",
    "print '-------------------------'\n",
    "print a  #并不影响a 本身"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
